package com.marry.multidatasource.controller;

import com.github.pagehelper.PageInfo;
import com.marry.multidatasource.model.RequestItem;
import com.marry.multidatasource.model.bd.RightOrProof;
import com.marry.multidatasource.service.OwnacaseService;
import com.marry.multidatasource.util.Cardhelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ml
 * @create 2018-01-06--20:39
 */
@RestController
public class OwandcaseController {

    @Autowired
    private OwnacaseService ownacaseService;

    /**
     * 通过名称查询
     * @param requestItem
     * @return
     */
    @GetMapping("/getRightsByName")
    public Map<String,Object> getRightsByName(RequestItem requestItem){
        System.out.println(requestItem);
        Map<String,Object> map=new HashMap<String,Object>(2);

        List<RightOrProof> rights=ownacaseService.getRightsOrProofByName(requestItem);
        PageInfo pageInfo=new PageInfo(rights);
        System.out.println("pageinfo.count"+pageInfo.getTotal());
        System.out.println("pageinfo.pages"+pageInfo.getPages());
//        Integer count=ownacaseService.selectCountByNameLcCn(requestItem);
        System.out.println("count = " + pageInfo.getTotal());
        map.put("data",rights);
        map.put("pages",pageInfo.getPages());
        return map;
    }

    /**
     * 通过产权证号或者房屋坐落
     * @param requestItem
     * @return
     */
    @GetMapping("/getRightsByLocationOrCqzh")
    public Map<String,Object> getRightsByLocationOrCqzh(RequestItem requestItem){
        System.out.println("OwandcaseController.getRightsByCertNo");
        System.out.println("requestItem = [" + requestItem + "]");
        Map<String,Object> map=new HashMap<String, Object>(2);

        List<RightOrProof> rightOrProofList=ownacaseService.getRightsByLocationOrCqzh(requestItem);
//        Integer count=ownacaseService.selectCountByNameLcCn(requestItem);
        PageInfo pageInfo=new PageInfo(rightOrProofList);
        System.out.println("count = " + pageInfo.getTotal());
        map.put("data",rightOrProofList);
        map.put("pages",pageInfo.getPages());


        return map;

    }

    @RequestMapping("/getRightsByCertno")
    public Map<String,Object> getRightsByCertno(RequestItem requestItem){
        System.out.println("OwandcaseController.getRightsByCertno");
        System.out.println("requestItem = [" + requestItem + "]");
        Map<String,Object> map=new HashMap<String, Object>(2);
        if(requestItem.getCertno()!=null){

            String certno=requestItem.getCertno();
            List<String> certnos=new ArrayList<String>();
            addCertno(certnos,certno,requestItem);
            if(certnos.size()!=0){
                requestItem.setCertnos(certnos);
                requestItem.setCertno(null);
            }
        }else if(requestItem.getCertnos()!=null){
            List<String> certnos=new ArrayList<String>();
            for(String certno:requestItem.getCertnos()){

                addCertno(certnos,certno,requestItem);

            }
            if(certnos.size()!=0){
                requestItem.setCertnos(certnos);
                requestItem.setCertno(null);
            }
        }
        List<RightOrProof> rightOrProofList=ownacaseService.getRightsByCertno(requestItem);
//        Integer count=ownacaseService.selectCountByCertno(requestItem);
        PageInfo pageInfo=new PageInfo(rightOrProofList);
        System.out.println("count = " + pageInfo.getTotal());
        map.put("data",rightOrProofList);
        map.put("pages",pageInfo.getPages());

        return map;


    }

    private void addCertno(List<String> certnos, String certno, RequestItem requestItem) {
        if(Cardhelper.isIdentityId(certno.toUpperCase())){
            if(!certno.equals(certno.toLowerCase()) || !certno.equals(certno.toUpperCase())){
//                    包含X
                certnos.add(certno.toUpperCase());
                certnos.add(certno.toLowerCase());

            }else{
                certnos.add(certno);
            }
            if(requestItem.getIs15()!=null){
                certnos.add(Cardhelper.newCodeToOld(certno));
            }


        }
    }
}
